//
//  SendVideoPostsController.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/26.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

class SendVideoPostsController: BaseViewController , UITextViewDelegate{
    fileprivate var bottomView:PostsToolView!
    fileprivate var scrollerView:UIScrollView!
    fileprivate var keyBoardHeight:CGFloat = 0
    fileprivate var sectionBtn:UIButton!
    
    fileprivate var textView:UITextView!
    fileprivate var textHintLab:UILabel!
    fileprivate var textCountLab:UILabel!
    fileprivate var textCount = 5000
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.initView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        NotificationCenter.default.addObserver(self, selector:#selector(self.keyBoardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector:#selector(self.keyBoardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    
    func keyBoardWillShow(_ note:Notification){
        KeyBoardUtils.shared.keyBoardWillShow(note, showView: self.bottomView) { (keyBoardHeight) in
            self.keyBoardHeight = keyBoardHeight
            if self.textView.isFirstResponder{
                self.updateTextViewFrame()
            }
            self.bottomView.transform = CGAffineTransform(translationX: 0,y: -(keyBoardHeight)) //让view跟随键盘弹起来
        }
    }
    
    func keyBoardWillHide(_ note:Notification){
        KeyBoardUtils.shared.keyBoardWillHide(note, hideView: self.bottomView) {
            self.scrollerView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
        }
    }
    
    fileprivate func initView(){
        self.navigationTitle = "发视频帖"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.setTextBarButton("发布", action: #selector(self.senPostsClick), target: self)
        self.automaticallyAdjustsScrollViewInsets = false
        
        self.scrollerView = UIScrollView.init(frame: CGRect.init(x: 0, y: NavigationH, width: AppWidth, height: AppHeight - NavigationH - 60))
        self.scrollerView.backgroundColor = UIColor.white
        self.scrollerView.keyboardDismissMode = .onDrag
        self.scrollerView.alwaysBounceVertical = true
        self.view.addSubview(self.scrollerView)
        
        let releaseHintLab = UILabel.init(frame: CGRect.init(x: 21, y: 20, width: 40, height: 17))
        releaseHintLab.font = UIFont.LightDefaultFont(12)
        releaseHintLab.text = "发布于"
        releaseHintLab.textColor = UIColor.m_102Color()
        self.scrollerView.addSubview(releaseHintLab)
        
        self.sectionBtn = UIButton.init(type: .custom)
        self.sectionBtn.frame = CGRect.init(x: releaseHintLab.frame.maxX + 15 , y: 13.5, width: AppWidth - releaseHintLab.frame.maxX - 15 - 20 - 7 - 15 , height: 30)
        self.sectionBtn.setTitleColor(UIColor.m_102Color(), for: UIControlState.normal)
        self.sectionBtn.setTitle("请选择板块", for: UIControlState.normal)
        self.sectionBtn.contentHorizontalAlignment = .right
        self.sectionBtn.titleLabel?.font = UIFont.LightDefaultFont(12)
        self.scrollerView.addSubview(self.sectionBtn)
        
        let nextImg = UIImageView.init(frame: CGRect.init(x: AppWidth - 20 - 12, y: 23, width: 7, height: 12))
        nextImg.image = UIImage.init(named: "next_ico")
        self.scrollerView.addSubview(nextImg)
        
        let lineView_one = UIView.init(frame: CGRect.init(x: 20, y: releaseHintLab.frame.maxY + 20, width: AppWidth - 20, height: 1))
        lineView_one.backgroundColor = UIColor.m_lineColor()
        self.scrollerView.addSubview(lineView_one)
        
        self.textView = UITextView.init(frame: CGRect.init(x: 20, y: lineView_one.frame.maxY + 10, width: AppWidth - 40, height: 100))
        self.textView.autocorrectionType = .no
        self.textView.font = UIFont.RegularDefaultFont(15)
        self.textView.textColor = UIColor.m_textDeepColor()
        self.textView.delegate = self
        self.textView.isScrollEnabled = false
        self.textView.layoutManager.allowsNonContiguousLayout = false
        self.scrollerView.addSubview(self.textView)
        
        self.textHintLab = UILabel.init(frame: CGRect.init(x: 5, y: 7, width: 260, height: 20))
        self.textHintLab.font = UIFont.LightDefaultFont(15)
        self.textHintLab.textColor = UIColor.m_placeholderColor()
        self.textHintLab.text = "请输入描述（最多500字）"
        self.textView.addSubview(self.textHintLab)
        
        self.bottomView = PostsToolView.init(isShowVideo: false, frame: CGRect(x: 0, y: AppHeight - 50, width: AppWidth, height: 50))
        self.view.addSubview(self.bottomView)
    }
    
    @objc fileprivate func senPostsClick(){
        
    }
    
    
    fileprivate func updateTextViewFrame(){
//        let height = StringSizeEngine.calculateStringHeight(textView.text, width: AppWidth - 50 , font: textView.font!)
//        
//        if height > 80.0{
//            UIView.animate(withDuration: 0.3, animations: {
//                self.textView.frame = CGRect.init(x: self.textView.frame.minX, y: self.textView.frame.minY, width: self.textView.frame.size.width, height: height + 10)
//            })
//        }else{
//            UIView.animate(withDuration: 0.3, animations: {
//                self.textView.frame = CGRect.init(x: 20, y: self.titleTF.frame.maxY + 26, width: AppWidth - 40, height: 100)
//            })
//        }
//        UIView.animate(withDuration: 0.3) {
//            self.chooseImagesView.frame = CGRect.init(x: 20, y: self.textView.frame.maxY + 10, width: AppWidth - 40, height: self.chooseImagesView.frame.size.height)
//            self.scrollerView.contentSize = CGSize.init(width: AppWidth, height: self.chooseImagesView.frame.maxY + 20)
//            if self.textView.frame.maxY > self.keyBoardHeight  && height > 80{
//                self.scrollerView.setContentOffset(CGPoint.init(x: 0, y: (self.textView.frame.maxY - self.keyBoardHeight)), animated: false)
//            }else{
//                self.scrollerView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
//            }
//        }
    }
}
